//创建显示产品的表格
function creatDisGoodsGrid(){
  // var allGoods = findGoods();
   
  // var gridStore = new Ext.data.JsonStore({data: allGoods,fields:["id","goodsPic","goodsName","goodsPrice"]});
  
  var columns = [
       new Ext.grid.RowNumberer(),
      {header:"goodsId",dataIndex:"id",hidden: true},
      {header:"cms.td.header.goodsPic",dataIndex:"goodsPic",renderer:renderGoodsPic,width:120},
      {header:"cms.td.header.goodsName",dataIndex:"goodsName",renderer:renderGoodsName},
      {header:"cms.td.header.goodsPrice",dataIndex:"goodsPrice",width: 80,renderer:renderGoodsPrice},
      {header:"cms.td.header.unit",dataIndex:"goodsPriceUnit",width:40,sortable: false,renderer:renderUnit},
      {header:"cms.td.header.goodsCount",dataIndex:"goodsCount",renderer:renderDataCount,width: 130,renderer:renderGoodsCount},
      {header:"cms.td.header.goodsSoldCount",dataIndex:"goodsHasSoldCount",width: 80,renderer:renderGoodsSoldCount},
      {header:"cms.td.header.goodsRestCount",dataIndex:"goodsRestCount",width: 130,renderer:renderGoodsRestCount},
      {header:"cms.td.header.goodsCart",dataIndex:"goodsChart",width:80,renderer:renderGoodsCart}
  ];
   i18nColM(columns);
   
   var title = $i18n("cms.label.disGoods.table.title");
   var colM = new Ext.grid.ColumnModel(columns);
   
   var tableObj = {};
   tableObj.height = 400;
   tableObj.width = 800;
   tableObj.colM = colM;
   tableObj.fields = ["id","goodsPic","goodsName","goodsPrice","goodsPriceUnit","goodsCount","goodsHasSoldCount","goodsRestCount","goodsChart"];
   tableObj.gridId = "disGoodsDiv";
   tableObj.title = title;
   tableObj.pageSize = 3;
   $.splitPage(tableObj,"findGoods.action");
   $("#contentDiv").css("height","400");
   $("#contentDiv").css("width","800");
}


//渲染产品名称
function renderGoodsName(value){
   return "<div style='height: 100;vertical-align: bottom;padding-top: 40px'>" + value + "</div>";
}
//渲染产品名称
function renderGoodsPrice(value){
   return "<div style='height: 100;vertical-align: bottom;padding-top: 40px'>" + value + "</div>";
}
//渲染币种
function renderUnit(value){
   return "<div style='height: 100;vertical-align: bottom;padding-top: 40px'>" + value + "</div>";
}

//渲染产品卖出数量
function renderGoodsSoldCount(value,cellMeta,record){
   var id = record.data["id"];
   return "<div style='height: 100;vertical-align: bottom;padding-top: 40px' id='hasSoldCount" + id + "'>" + value + "</div>";
}

//渲染产品剩余数量
function renderGoodsRestCount(value,cellMeta,record){
   var id = record.data["id"];
   return "<div style='height: 100;vertical-align: bottom;padding-top: 40px' id='restCount" + id + "'>" + value + "</div>";
}


//渲染购物车
var goodsData = {};
function renderGoodsCart(value,cellMeta,record){
   var goodsId = record.data["id"];
   return "<img src='image/goodsCart.jpg' width='60px' height='100px' style='cursor: pointer' onclick=openGoodsCartDialog('goodsCartDiv','" + goodsId + "')></img>";
}

//渲染产品名称数量
function renderGoodsCount(value){
   return "<div style='height: 100;vertical-align: bottom;padding-top: 40px'>" + value + "</div>";
}

//渲染产品名称数量
function renderGoodsCartCount(value,cellMeta,record){
   var id = record.data["id"];
   return "<div style='height: 100;vertical-align: bottom;padding-top: 40px'><input id='" + id + "'onclick='$(this).focus()' onpropertychange='validatePositiveNumberForCart(this)' style='width:60px' type='text' value='" + value + "'/><input  type='hidden' value='" + value + "'/></div>";
}

//对商品图片路径进行渲染
function renderGoodsPic(value,cellMeta,record){
  var id = record.data["id"];
  var imgObj = "<img id='"+id+"'onclick='forwardToSingleGoodsPage(this)' onmouseover='addBorder(this);' onmouseout='removeBorder(this);' src='" + value + "'" + " width='100px' height='100px' style='border: 1px solid #ccc;cursor: pointer'/>";
  return imgObj;
}

//渲染数量
function renderDataCount(value){
  if(isEmpty(value)){
    return "0";
  }
  return value;
}
//转向显示单个产品的页面
function forwardToSingleGoodsPage(curObj){
   var goodsNo = $(curObj).attr("id");
   load("contentDiv","disSingleGoods.html",false,function(){
      createDisSingleGoodsInfoPage(goodsNo);
   });
}


//查找商品信息
function findGoods(){
    var goods = null;
    $.ajax({
      type:"POST",
      url:"findGoods.action",
      async: false,
      success: function(result){
        goods = strToJson(result);
      },
      error: function(request,error,status){
         showErrorMsg(error);
      }
   });
   return goods;
}

$(document).ready(function(){
   creatDisGoodsGrid();
});